<template>
  <el-dialog
    :visible="isShow"
    title="新增员工"
    width="750px"
    @close="closeEvent"
  >
    <el-form label-width="120px" :model="form" :rules="rules" ref="form">
      <el-form-item label="姓名:" prop="username"
        ><el-input placeholder="1-30个字符" v-model="form.username"></el-input
      ></el-form-item>
      <el-form-item label="手机:" prop="mobile"
        ><el-input placeholder="请输入手机号" v-model="form.mobile"></el-input
      ></el-form-item>
      <el-form-item label="入职时间:" prop="timeOfEntry">
        <el-date-picker type="date" v-model="form.timeOfEntry"></el-date-picker>
      </el-form-item>
      <el-form-item label="聘用形势:" prop="formOfEmployment"
        ><el-select placeholder="请选择" v-model="form.formOfEmployment">
          <el-option
            v-for="item in employees.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          ></el-option> </el-select
      ></el-form-item>
      <el-form-item label="工号:" prop="workNumber"
        ><el-input placeholder="1-20个字符" v-model="form.workNumber"></el-input
      ></el-form-item>
      <el-form-item label="部门:" prop="departmentName">
        <div class="tree-item" ref="treeItem">
          <el-input
            placeholder="请选择"
            v-model="form.departmentName"
            @focus="focusEvent"
            readonly
          ></el-input>
          <el-tree
            :data="treeData"
            v-if="treeShow"
            :props="{ label: 'name' }"
            @node-click="nodeClick"
          ></el-tree>
        </div>
      </el-form-item>
      <el-form-item label="转正时间:" prop="correctionTime"
        ><el-date-picker
          type="date"
          v-model="form.correctionTime"
        ></el-date-picker
      ></el-form-item>
    </el-form>
    <template>
      <div class="btn" style="text-align:center">
        <el-button type="primary" @click="submit">提交</el-button>
        <el-button @click="closeEvent">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
// 导入枚举文件
import employees from '@/assets/constant/employees.js'
// 导入获取部门接口api
import { companyDepartment } from '@/api/departments.js'
// 导入新增员工接口api
import { sysUserPost } from '@/api/employees.js'
export default {
  props: {
    isShow: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      treeShow: false, // 控制部门tree组件显示
      treeData: [], // 部门数据
      employees: employees, // 使用data中间存储
      form: {
        username: '', //	string	非必须		姓名
        mobile: '', //	string	非必须		手机号
        timeOfEntry: '', //	string	非必须		入职时间
        formOfEmployment: '', //	number	非必须		聘用形式
        workNumber: '', //	string	非必须		工号
        departmentName: '', //	string	非必须		组织名称
        correctionTime: '' //	string	非必须		转正时间
      },
      rules: {
        username: [{ required: true, message: '必填', trigger: 'change' }],
        mobile: [{ required: true, message: '必填', trigger: 'change' }],
        timeOfEntry: [{ required: true, message: '必填', trigger: 'change' }],
        formOfEmployment: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        workNumber: [{ required: true, message: '必填', trigger: 'change' }],
        departmentName: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        correctionTime: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  mounted () {
    // 原生全局点击事件监听
    document.addEventListener('click', this.clickEvent)
  },
  beforeDestroy () {
    // 销毁
    document.removeEventListener('click', this.clickEvent)
  },
  methods: {
    // 取消
    closeEvent () {
      this.$emit('update:isShow', false)
    },
    // 提交
    submit () {
      this.$refs.form.validate(async result => {
        if (result) {
          // 调用新增接口
          console.log(this.form)
          await sysUserPost(this.form)
          this.$message.success('新增成功')
          this.closeEvent()
          // this.$emit('getData', false)
        }
      })
    },
    // 部门input的聚焦事件
    async focusEvent () {
      this.treeShow = true
      // 调用接口前先判断有没有数据
      if (this.treeData.length === 0) {
        // 获取部门数据
        const res = await companyDepartment()
        this.treeData = this.changeData(res.data.depts, '')
      }
    },
    // 树形数据转换方法
    changeData (arr, str) {
      return arr.filter(item => {
        if (item.pid === str) {
          // 给每个数组元素都添加一个children
          item.children = this.changeData(arr, item.id)
          return true
        }
      })
    },
    // 树形控件点击时的事件
    nodeClick (data) {
      this.form.departmentName = data.name
    },
    // 监听点击事件
    clickEvent (e) {
      // 判断点击的区域是否在tree中
      if (this.isShow && !this.$refs.treeItem.contains(e.target)) {
        // 关闭tree
        this.treeShow = false
      }
      // console.log(this.$refs.treeItem.contains(e.target))
    }
  }
}
</script>

<style lang="scss" scoped>
// .btn {
//   text-align: center;
// }
</style>
